<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

include( "../global/utils.php");
if( isset( $_POST[ 'text' ] ) && formatInputLetter( $_POST[ 'text' ] ) != "" && isset( $_POST[ 'subject' ] ) && formatInputLetter( $_POST[ 'subject' ] ) != "" && isset( $_POST[ 'to' ] ) && is_numeric( $_POST[ 'reply' ] ) ) {
	$to_post = $_POST[ 'to' ];
	$message = formatInputLetter( $_POST[ 'text' ] );
	if( isset( $_POST[ 'faction' ] ) ) {
	    $query = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
	    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );

		$query = "SELECT ruler.name, ruler.ruler_id FROM ruler JOIN faction_member ON faction_member.ruler_id = ruler.ruler_id WHERE faction_id = " . $row[ 'faction_id' ] . " AND invited = 0 AND requested = 0";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		$i = 0;
		while ( $i < $num ) {
			if( mysql_result( $result, $i, "ruler_id" ) != $_SESSION[ 'ruler_id' ] ) {
				if( $to_post == "" ) {
					$to_post = mysql_result( $result, $i, "name" );
				}
				else {
					$to_post = $to_post . ", " . mysql_result( $result, $i, "name" );
				}
			}
			$i ++;
		}
	}
	
	$timestamp = time();
    $query = "SELECT turn, date FROM game";
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
    $query = "INSERT INTO messages ( subject, text, date, parent, turn, sender, timestamp ) VALUES ( '" . formatInputLetter( $_POST[ 'subject' ], true ) . "', " .
    		"'$message', " .
    		"'" . $row[ 'date' ] . "'," .
    		$_POST[ 'reply' ] . " ," . 
    		$row[ 'turn' ] . ", " . $_SESSION[ 'ruler_id' ] . ", $timestamp )";
    mysql_query( $query ) or die( "Query failed : " . mysql_error() );

    $query = "SELECT message_id " .
    		"FROM messages " .
    		"WHERE text = '$message' AND " .
    		" subject = '" . formatInputLetter( $_POST[ 'subject' ] ) . "' AND " .
    		"turn = " . $row[ 'turn' ] . " AND sender = " . $_SESSION[ 'ruler_id' ];
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$message_id = $row[ 'message_id' ];
	
	$i = 0;
	$to = strtok( $to_post, "," );
	while ( $to !== false ) {
		$send_to = trim( $to );
	    $query = "SELECT ruler_id FROM ruler WHERE name = '$send_to'";
	    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( isset( $row[ 'ruler_id' ] ) ) {
		    $query2 = "SELECT count(*) FROM message_ruler WHERE ruler_id = " . $row[ 'ruler_id' ] . " AND message_id = $message_id";
		    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			if( $row2[ 'count(*)' ] == 0 ) {
			    $query = "INSERT INTO message_ruler ( message_id, ruler_id ) VALUES ( $message_id, " . $row[ 'ruler_id' ] . " )";
			    mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			    if( $i > 0 ) {
			    	$status = $status . ", " . $send_to;
			    }
			    else {
					$status = translate( "Letter sent to" ) . ": " . $send_to;
			    }
			}
			$i ++;
		}
		$to = strtok( "," );
	}  
}
else {
	if( isset( $_POST[ 'text' ] ) ) {
		$_SESSION[ 'send_letter_text' ] = formatInputLetter( $_POST[ 'text' ] );
	}
	
	if( isset( $_POST[ 'subject' ] ) ) {
		$_SESSION[ 'send_letter_subject' ] = formatInputLetter( $_POST[ 'subject' ] );
	}

	if( isset( $_POST[ 'to' ] ) ) {
		$_SESSION[ 'send_letter_to' ] = formatInputLetter( $_POST[ 'to' ] );
	}
	$status = translate( 'Fill in to, subject and a message.' );
}
if( isset( $status ) ) {
	$_SESSION[ 'send_letter_status' ] = $status;
}
header( "Location: send_letter.php" );
?>
